package com.dcms.admin.controller;

import com.dcms.power.annotation.ArchivesLog;
import com.dcms.admin.bean.User;
import com.dcms.admin.service.IUserService;
import com.dcms.power.bean.Role;
import com.dcms.power.bean.UserRole;
import com.dcms.power.dao.UserRoleMapper;
import com.dcms.power.service.IRoleService;
import com.dcms.util.FileUtil;
import com.dcms.util.TimeUtil;
import com.dcms.util.layui.LayTable;
import com.dcms.util.ResultBean;
import com.dcms.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * \* @name: UserController
 * \* @author: 武佳伟丶
 * \* @date: 2018/2/11 0011
 * \* @tine: 22:33
 * \* @description: To change this template use File | Settings | File Templates.
 * \
 */
@Controller
@RequestMapping("admin/user")
public class UserController extends BaseController{

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @RequestMapping("display")
    public String listPage(){
        List<Role> roles = roleService.selectAll(null, null);
        request.setAttribute("roles", roles);
        return "admin/user/list";
    }

    @RequestMapping("query")
    @ResponseBody
    public LayTable<User> query(
            String param,
            User record,
            Integer limit,
            Integer page
    ){
        page = page == null ? 1 : page;
        limit = limit == null ? 12 : limit;

        PageHelper.startPage(page, limit);
        List<User> list = userService.selectAll(param, record);
        PageInfo<User> info = new PageInfo<User>(list);
        LayTable<User> bean = new LayTable<User>(info.getList(), (int)info.getTotal());
        return bean;
    }

    /**
     * @author: 武佳伟丶
     * @description: 删除，支持批量
     * @date: 14:39 2018/2/12 0012
     * @param: [id]
     * @return: com.dcms.util.ResultBean
     */
    @ArchivesLog(message = "删除用户")
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean deleteBatch(@RequestParam(value = "id[]", required = true)Integer[] id){
        int rows = userService.deleteBatch(id);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶
     * @description: 更新
     * @date: 18:55 2018/2/13 0013
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(User record){
        int rows = userService.updateByPrimaryKeySelective(record);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶
     * @description: 跳转到数据编辑页，用于新增数据
     * @date: 14:42 2018/2/18 0018
     * @param: [userId, request]
     * @return: java.lang.String
     */
    @RequestMapping("toinsert")
    public String toInsert(){
        List<Role> roles = roleService.selectAll(null, null);
        request.setAttribute("roles", roles);
        return "admin/user/edit";
    }

    /**
     * @author: 武佳伟丶
     * @description: 录入数据
     * @date: 14:52 2018/2/18 0018
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping("insert")
    @ResponseBody
    @ArchivesLog(message = "添加用户")
    public ResultBean insert(
            User record,
            String lock,
            String roleIds,
            @RequestParam(value = "file", required = false) MultipartFile file){
        Date now = new Date();
        //锁定状态
        if (StringUtil.isNotEmpty(lock)){
            if ("on".equals(lock)){
                record.setLocked(User.LOCKED_NO);
            } else {
                record.setLocked(User.LOCKED_YES);
            }
        }
        //头像
        String filePath = "/static/upload/avatar/" + TimeUtil.getStrFromDate(new Date(), "yyyyMMdd") + "/";
        String avatar = FileUtil.uploadImage(request, filePath, file, true);
        if (avatar == null){
            avatar = "/static/build/images/avatar.jpg";
        }
        record.setAvatar(avatar);
        //创建时间
        record.setCtime(now);
        int rows = userService.insertSelective(record);
        if (rows > 0){
            int userId = record.getUserId();
            if (StringUtil.isNotEmpty(roleIds)){
                String[] roleId = roleIds.split(",");
                for (int i = 0; i < roleId.length; i ++){
                    UserRole userRole = new UserRole();
                    userRole.setCreator(userId);
                    userRole.setCtime(now);
                    userRole.setRoleId(Integer.valueOf(roleId[i]));
                    userRole.setUpdateTime(now);
                    userRole.setUserId(userId);
                    userRoleMapper.insertSelective(userRole);
                }
            }
            return  new ResultBean();
        }
        return new ResultBean();
    }

    @ArchivesLog(message = "上传头像")
    @RequestMapping
    @ResponseBody
    public ResultBean upload(MultipartFile file){
        ResultBean result = new ResultBean();

        return result;
    }
}